/*
	All upload from soft ware will use flash or quick-iframe
	Then most of pages are used ajax
	
	FLASH UPLOAD:
	As issue of session in Flash, I use hashkey to pass this error.
	I have read about HTML5 for ajax upload but it is not good at least for a button browser
		
	
	FORM POST
	I use ajax with json to run a form.
	The start page return :
		{
			content: your_text
		}
	The error set in .form-message:
		{
			message: your_text	
		}
		
	The error show alert:
		{
			alert: your_text
		}
	The direct link ( when form success ):
		{
			redirect: your_url
		}

	Reload page:
		{
			reload: 'yes'
		}
	
	
	ERROR LOGIN
	if _json set into request then return:
	{
		error_login: true
	}	
	
	if _ajax set into request then return a quick login
	
	CONCLUSION:
		Don't forget set _json or _ajax for your plugins :">	
	
*/
function open_login(){
/*	$.createLightBox($.Extend({
		title: options.title||'Trình duyệt nhanh: ',
		callback: function(){
			$(this)
				.htm("<table width='100%'>"
					+"<tr><td></td>"
					+"<td></td></tr>"
					+"<tr><td></td>"
					+"<td></td></tr>"
					+"</table>");
		}			
	},options||{} ));*/
	location.href = baseURL+'/Login';	
}

var APP_CALLBACK ={};
(function($){
	
	/* ajax loading*/
	$("<div id='ajax-load'>Đang tải dữ liệu ... </div>").firstTo("body");
	
	window.ajax_show = function(){
		move_center();
		$("#ajax-load").css("display:block");	
	};

	window.ajax_hide = function(){
		$("#ajax-load").css("display:none");	
	};	
	
	function move_center(){
		$(function(){
			$("#ajax-load").css({
				left: $(window).width()/2 - $("#ajax-load").width()/2 + "px"
			});	
		});	
	}
	
	$(window).addEvent("scroll resize", move_center);
	
	/* Remove before load */
	window.app_clean = function(){
		$(".app-auto-hidden").remove();	
	};	
	
	function updateContent( id, json_data){
		if( id == null ){
			id = '#main-content';
		}
		if( 'error_login' in json_data ){
			$.Alert('Bạn đã bị thoát ra',function(){
				open_login();
			});
		}else if( 'alert' in json_data ){
			$.Alert( json_data['alert'] );		
		}else if( 'url' in json_data ){
			location.href = json_data['url'];
		}else if( 'redirect' in json_data ){
			app_clean();
			if( location.hash == json_data['redirect'] ){
				app_load();				
			}else{
				location.hash = json_data['redirect'];
			}			
		}else if( 'content' in json_data ){
			app_clean();

			$(id)
				.htm( json_data['content'] )
				.each(function(){
					app_callback.call(this)
				});
		}else if( 'reload' in json_data ){
			app_load();	
		}else if( 'message' in json_data ){
			if( $(id).get('tagName') == 'FORM' ){
				document.body.scrollTop = Math.max(0,$(id).find(".form-message").htm( json_data['message'] ).parent(0).top()-50);					 
			}else{
				$.Alert( json_data['message'] );	
			}
		}else if( 'callback' in json_data ){
			eval(json_data['callback']);		
		}	
	}	
	
	function app_callback(){ 
		for( var x in APP_CALLBACK ){
			APP_CALLBACK[x].call(this);
		}
	}
	
	/* Load content of action */
	window.app_load = function(){
		app_clean();
		var url ='',m;
		if( location.hash == '' || location.hash == '#'){
			url = baseURL+'/Home';
			$(".menu .main-link").removeClass('menu-active');
			$(".menu .m-home").addClass('menu-active');			
		}else{	
			url = baseURL+'/' + location.hash.split('#')[1];			
			if( m = location.hash.match(/#([a-z0-9]+)/i) ){
				$(".menu .main-link").removeClass('menu-active');
				$(".menu .m-"+m[1].toLowerCase()).addClass('menu-active');
			}
		}	
		
		if( window.APP_AJAX ){
			window.APP_AJAX.stop();
		}
		
		window.APP_AJAX = $.Ajax( url, {
			cache: false,
			create: ajax_show,
			complete: ajax_hide,
			error: function(){
				$.Alert("Không thể tải dữ liệu.");
				ajax_hide();
			},
			data:{
				_json: 'yes'	
			},
			success: function( data ){
				try{
					eval("var result = "+data+";");
				}catch(e){
					$("#main-content")
						.htm( data )
						.each(function(){
							app_callback.call(this)
						});
					return false;
				}
				updateContent("#main-content", result);
			}		
		});	
	};

	window.ajax_load = function( url ){
		$.Ajax( url, {
			cache: false,
			create: ajax_show,
			complete: ajax_hide,
			error: function(){
				$.Alert("Không thể tải dữ liệu.");
				ajax_hide();
			},
			data:{
				_json: 'yes'	
			},
			success: function( data ){
				try{
					eval("var result = "+data+";");
				}catch(e){
					$.Alert(data);
					return false;
				}
				updateContent("#main-content", result);
			}		
		});	
	};
	
	/* Tu dong load tu hash */	
	$(app_load);	
	
	/* Tu dong hash change */
	if( 'onhashchange' in window ){
		$(window).addEvent("hashchange",app_load);
	}else{
		window.cross_hashchange_link = null;
		setInterval(function(){
			if( window.cross_hashchange_link != location.hash ){
				window.cross_hashchange_link = location.hash;
				app_load();
			}		
		},5);
	}
	
	//fixed for hashchange
	$(document).onClick(function( event ){
		if( !event.target.tagName || event.target.tagName != "A"){
			return true;
		}
		
		var hash = $(event.target).getAttr("href")+"";
		if( hash.match(/^#/i) ){
			if( hash == location.hash ){
				app_load();
			}
		}	
	});
	
	
	/*
		load a action normally	
	*/
	window.ajax_form = function( form, is_main ){
		$( form ).submitAjax({
			data:{
				_json: 'yes'	
			},		
			create:	ajax_show,
			complete: ajax_hide,
			error: function(){
				$.Alert("Không thể tải dữ liệu.");
				ajax_hide();
			},			
			success: function(data){
				try{
					eval("var json_data = "+data+";");
				}catch(e){
					$.Alert( data );
					return false;
				}
				if( json_data.close ){
					$(form).parent('.lightbox').remove();
					delete json_data.close;
				}
				updateContent(is_main ? null : form,json_data);
			}
		});
		return false;
	};	

	APP_CALLBACK['ajaxform'] = function(){
		$(this)
			.find('form.ajaxform')
			.each(function(){
				var form = this;
				$(this)
					.onSubmitAjax({
						data:{
							_json: 'yes'	
						},		
						create:	ajax_show,
						complete: ajax_hide,
						error: function(){
							$.Alert("Không thể tải dữ liệu.");
							ajax_hide();
						},			
						success: function(data){
							try{
								eval("var json_data = "+data+";");
							}catch(e){
								$.Alert( data );
								return false;
							}
							
							if( json_data.close ){
								$(form).parent('.lightbox').remove();
								delete json_data.close;
							}
							
							updateContent(form,json_data);
						}
					});	
			});		
	};
	
	
	window.load_frame = function( url, options ){
		load_inframe( url, options );	
	};

	window.load_inframe = function( url, options ){
		options = options||{};
		var data = {
				_json: 'yes',
				remove_layout: 'yes'				
			};
			
		if( options['dataForm'] ){
			var data1 = $(options['dataForm']).query();
			data = $.Extend(data,data1);
		}
		
		$.Ajax(url,{
			cache:false,
			data: data,
			create: ajax_show,
			complete: ajax_hide,
			success: function( data ){
				try{
					eval("var result = "+data+";");
				}catch(e){
					$.createLightBox($.Extend({
						title: options.title||'Trình duyệt nhanh: ',
						callback: function(){
							$(this)
								.htm( data )
								.each(function(){
									app_callback.call(this)
								});
						}			
					},options||{} ));
					return false;
				}					

				if( result['error_login'] ){
					$.Alert('Bạn đã bị thoát ra');
				}else if( 'alert' in result ){
					$.Alert( result.alert );		
				}else if( 'location' in result ){
					location.href = result.location;
				}else if( 'redirect' in result ){
					app_clean();
					if( location.hash == result.redirect ){
						app_load();				
					}else{
						location.hash = result.redirect;
					}			
				}else if( 'content' in result ){
					$.createLightBox($.Extend({
						title: options.title||'Trình duyệt nhanh: ',
						callback: function(){
							$(this)
								.htm( result.content )
								.each(function(){
									app_callback.call(this)
								});
						}			
					},options||{} ));
				}						
			}				
		});	
	};	
	
	
	window.load_html = function( selector, options ){
		options = options||{};
		$.createLightBox($.Extend({
			title: options.title||'Trình duyệt nhanh: ',
			callback: function(){
				if( $(selector).size() > 0 ){
					$(this)
						.htm('')
						.append( $(selector).k(0).cloneNode(true) );
				}		
			}			
		},options||{} ));	
	
	};	
	
	
	/* init some script */
	APP_CALLBACK["init_check"] = function(){
		$(this)
			.find(".x-check-container")		
			.onMouseenter(function(){
				$(this)
					.find(".x-check")
					.css({
						 border:'1px solid #BCBCBD',
						'border-left-width':'0px',
						padding:'4px',
						height:'13px',
						background: '#E6E6E8'
					});
				$(this)
					.find(".x-check-options")			
					.css({display:'block'})
			})
			.onMouseleave(function(){
				$(this)
					.find(".x-check")
					.css({
					    border:'none',
						padding:'0px',
						height:'auto',
						background: 'transparent'
					});			
				$(this)
					.find(".x-check-options")			
					.css({display:'none'})
			});
			
			
		//menu options popup	
			
		$(this)
			.find(".x-options-container")		
			.onMouseenter(function(){
				$(this)
					.find(".x-button")
					.addClass('x-options-hover');
				$(this)
					.find(".x-options")			
					.css({display:'block'})
			})
			.onMouseleave(function(){
				$(this)
					.find(".x-button")
					.removeClass('x-options-hover');			
				$(this)
					.find(".x-options")			
					.css({display:'none'})
			})
			.child(".x-options")
			.child(".x-options-item")
			.onMouseenter(function(){
				 $(this)
					.child(".x-options-child")
					.css({
						display: 'block',
						right: (this.parentNode.offsetWidth-2)+'px'
					});			
			})
			.onMouseleave(function(){
				 $(this)
					.child(".x-options-child")
					.css({display: 'none'});
			
			})		
	};	
	
	/* user multi check */
	APP_CALLBACK["user_check"] = function(){
		$(this)
			.find(".x-select-users,.x-select-user")
			.each(function(){
				var obj = this, single = this.className == 'x-select-user';
				$(obj).css({display:'inline-block'});
				
				var ip = document.createElement('input');
				$(ip).css({border:'none',outline:'none',width:'100px'});
				$(obj).onClick(function(){
					ip.focus();
				});
				
				var ipWrap = $("<div style='display:inline-block;position:relative'></div>");
				var suggest = $("<div style='display:none;position:absolute;z-index:100'>"
					+"<table cellpadding='0' cellspacing='0'><tr>"
					+"<td style='background:#fff;min-width:150px;border:1px solid #ccc;'></td></tr></table></div>");
				ipWrap
					.append(ip)
					.append( suggest.k(0) )
					.appendTo(obj);
			
				suggest
					.css({
						top: ip.offsetHeight+'px',
						left: '0px'	
					});			
			
				$(ip)
					.onBlur( function(){
						suggest.css({
							display:'none'
						});
					})
					.onKeydown(function(e){
						if( e.KEY_ENTER ){
							e.preventDefault();
							var nodes = suggest.find('td > div').nodes;
							$(nodes[ip.idx]).fire('mousedown');
							suggest.css({
								display:'none'
							});													
						}	
					})
					.onKeydown(function(e){
						if( !(e.KEY_DOWN || e.KEY_UP) )
							return;
						if( ip.idx === undefined )
							ip.idx = -1;
						var nodes = suggest.find('td > div').nodes;
						if( nodes.length == 0 )
							return;
						ip.idx  = ( ip.idx+(e.KEY_DOWN ? 1 : -1)) % nodes.length;
						if( ip.idx <= -1 ) ip.idx = nodes.length - 1;	
						
						$(nodes).css({
							background:'#fff'
						});
						
						$(nodes[ip.idx])
							.css({
								background: '#eee'
							});						
						
					})					
					.onKeyup(function( e ){
						if( e.KEY_DOWN || e.KEY_UP )
							return;
						suggest.css({
							//display:'none'
						});		
						
						$.Ajax(baseURL+'/User/Select/Suggest?s=' + this.value,{
							success: function( data ){
								try{
									eval("var a = "+data+";");
								}catch(e){
									return false;
								}

								if( !a.content || a.content.length == 0 ){
									return false;
								}
								
								suggest
								.find('td')
								.htm('');
								td = suggest.find('td').k(0);
								for(var i =0; i < a.content.length; i++ ){
									var u = a.content[i];
									$('<div style="padding:5px">'
										+'<a style="display:inline-block">'
											+ u.username 
											+'<span style="display:inline-block;padding:0px 10px;font-size:10px;font-style:italic;color:#444">'+u.department_title+'</span>'
										+'</a>'										
										+'</div>')
										.onMouseenter(function(){
											$(this)
												.css({
													background:'#eee'
												})
										})
										.onMouseleave(function(){
											$(this)
												.css({
													background:'#fff'
												})
										})
										.set('xname',$(obj).getAttr('x-name'))
										.set('xvalue',u.ID)
										.set('xlabel',u.username)
										.onMousedown(function(){
											if(single){
												$(obj)
													.find('span.item')
													.remove()
											}
										
										
											$("<span class='item'>"
												+"<input type='hidden' name='"+this.xname+"' value='"+this.xvalue+"'/>"
												+"<a>"+this.xlabel+"</a>"
												+"&nbsp;&nbsp;<span class='x' onclick='$(this.parentNode).remove()'></span>"
												+"</span>")
											.beforeTo(ip);										
											ip.value='';
											ip.focus();
										})	
										.appendTo(td);
								}
								
								ip.idx = 0;
								suggest.css({
									display:'inline-block',
									left: ip.offsetLeft+'px',
									top: (ip.offsetTop+ip.offsetHeight)+'px'
								});
								suggest.find('td > div:first-child')
									.css({
										background:'#eee'
									})
							}						
						});						
					});
				
				$('<a class="x" title="Xóa hết"></a>')
					.appendTo( obj )
					.css({
						position:'absolute',
						top:'5px',
						right:'5px'
					})
					.onClick(function(){
						$(obj)
							.find('span.item')
							.remove()					
					});
					
				$('<a class="x-select-users-icon" title="Chọn người dùng"></a>')
					.afterTo( obj )
					.onClick(function(){
						$.createLightBox({
							title: $(obj).getAttr('x-title')||'Chọn người dùng',
							css_content: {
								width: '300px',
								height: '350px',
								overflow:'auto',
								position: 'relative'
							},
							callback: function(){
								var elem = this, 
									name = $(obj).getAttr('x-name')
									url = baseURL+'/User/Select/Multicheck?name='+name;
								$.Ajax( url,{
									data: {
										is_checkbox: single ? 0 : 1
									},
									success: function( data ){
										try{
											eval("var result = "+data+";");
										}catch(e){
											$.Alert( data );
											return false;
										}			
										if( result['error_login'] ){
											$.Alert('Bạn đã bị thoát ra');
										}else if( 'alert' in result ){
											$.Alert( result.alert );		
										}else if( 'location' in result ){
											location.href = result.location;
										}else if( 'redirect' in result ){
											app_clean();
											if( location.hash == result.redirect ){
												app_load();				
											}else{
												location.hash = result.redirect;
											}			
										}else if( 'content' in result ){	
											$(elem.parentNode.parentNode)
												.onClick(function( event ){
													//event.preventDefault();
													event.stopPropagation();										
												});
												
											$(elem)
												.htm( result.content )
												.find("input[type=checkbox],input[type=radio]")	
												.each(function(){
													var el = this;
													$(obj)
														.find("input[name]")
														.each(function(){
															if( this.value == el.value ){
																el.checked = true;
																return false;
															}							
														})
												})
												.onClick(function(){												
													$(obj)
														.find('span.item')
														.remove();

													$(elem)
														.find("input:checked")
														.each(function(){
															if( this.name == name ){
																$("<span class='item'>"
																	+"<input type='hidden' name='"+name+"' value='"+this.value+"'/>"
																	+"<a>"+$(this).getAttr('x-label')+"</a>"
																	+"&nbsp;&nbsp;<span class='x' onclick='$(this.parentNode).remove()'></span>"
																	+"</span>")
																.firstTo(obj)
																.find(".close")
																.onClick(function( event ){
																	event.preventDefault();
																	event.stopPropagation();
																	$(this.parentNode).animate({
																		opacity:0
																	},{
																		duration:250,
																		callback:function(){
																			$(this).remove();
																	}});														
																})	
															}		
														})	
														
												});
										}		
									}						
								});					
							}			
						});	
					});			
			})		
			.find(".close")
			.onClick(function( event ){
				event.preventDefault();
				event.stopPropagation();
				$(this.parentNode).animate({
					opacity:0
				},{
					duration:250,
					callback:function(){
						$(this).remove();
				}});														
			});	
	};
	
	
	

	APP_CALLBACK['editor'] = function(){
		$(this)
			.find(".x-editor")
			.each(function(){
				if( this.convertEditor )
					return true;
													
				//new nicEditor().panelInstance(this);
				//new nicEditor({fullPanel : true}).panelInstance(this);
				new nicEditor({maxHeight: $(this).parent('.lightbox-content').size() > 0 ? 200 : 800, iconsPath : baseURL+'/style/plugins/editor/icons.gif'}).panelInstance(this);
				//new nicEditor({buttonList : ['fontSize','bold','italic','underline','strikeThrough','subscript','superscript','html','image']}).panelInstance('area4');
				//new nicEditor({maxHeight : 100}).panelInstance('area5');				
				this.convertEditor = true
			});	
	};
	
	APP_CALLBACK['view_profile'] = function(){
		$(this)
			.find("a.userlink")
			.onClick(function( event ){
				load_inframe(baseURL+'/' + this.href.split('#')[1],{ title: 'Thông tin cá nhân của <u>' + this.innerHTML +'</u>'});			
				event.preventDefault();
				event.stopPropagation();
			});
	}
	
	APP_CALLBACK['check_all'] = function(){
		$(this)
			.find(".x-check-container > .x-check > .x-checkall")
			.onClick(function(){
				$(this)
					.parent('form')
					.slice(0,1)
					.find('input.x-checkall')
					.set('checked',this.checked);
			});

		$(this)
			.find(".check-all")
			.onClick(function(){
				$(this)
					.parent('form')
					.slice(0,1)
					.find('input.x-checkall')
					.set('checked',true);
			});
		$(this)
			.find(".uncheck-all")
			.onClick(function(){
				$(this)
					.parent('form')
					.slice(0,1)
					.find('input.x-checkall')
					.set('checked',false);
			});
		$(this)
			.find(".check-read")
			.onClick(function(){
				$(this)
					.parent('form')
					.slice(0,1)
					.each(function(){
						$(this)
							.find('tr.x-read input.x-checkall')
							.set('checked',true);
						$(this)
							.find('tr.x-noread input.x-checkall')
							.set('checked',false);							
					});	
			});
		$(this)
			.find(".check-unread")
			.onClick(function(){
				$(this)
					.parent('form')
					.slice(0,1)
					.each(function(){
						$(this)
							.find('tr.x-noread input.x-checkall')
							.set('checked',true);
						$(this)
							.find('tr.x-read input.x-checkall')
							.set('checked',false);							
					});
			});		
	};
		
	/* Action delete with confirm */
	window.module_delete = function( url, id ){
		
		if( id === undefined ){
			$.Confirm("Bạn có chắc muốn xóa không ?",function(){
				remove_frame(this);
				ajax_load( url );
			});
			return
		}	
		
		var params = $('#'+id).query();
		
		if( $('#'+id).queryString() == '' ){
			return $.Alert('Chưa có bản ghi nào được chọn');
		}
		
		params['_json'] = 'yes';
				
		$.Confirm('Bạn có chắc muốn xóa hết không ?',function(){
			remove_frame(this);
			$.Ajax( url, {
				cache: false,
				create: ajax_show,
				complete: ajax_hide,
				error: function(){
					$.Alert("Không thể tải dữ liệu.");
					ajax_hide();
				},
				data:params,
				success: function( data ){
					try{
						eval('var result = '+data+';');
					}catch(e){
						$.Alert(data);
						return false;
					}
					updateContent('#main-content', result);
				}		
			});	

		});
	};	
	
	window.select_user = function( elem ){
		var name = elem.getAttribute('x-name');
		$.Ajax(baseURL+'/User/Select/Check?name='+name,{
			success: function( data ){
				try{
					eval("var result = "+data+";");
				}catch(e){
					$.Alert( data );
					return false;
				}			
				if( result['error_login'] ){
					$.Alert('Bạn đã bị thoát ra');
				}else if( 'alert' in result ){
					$.Alert( result.alert );		
				}else if( 'location' in result ){
					location.href = result.location;
				}else if( 'redirect' in result ){
					app_clean();
					if( location.hash == result.redirect ){
						app_load();				
					}else{
						location.hash = result.redirect;
					}			
				}else if( 'content' in result ){
					$.createLightBox({
						css_content:{
							width:'300px',
							height:'300px'
						},
						title: 'Chọn người dùng',
						callback: function(){
							var c = this;
							$(this)
								.htm( result.content )
								.find(".tree-check")
								.onClick(function(){
									$(elem).htm("<input type='hidden' value='"+this.value+"' name='"+this.name+"'/><b>"+this.getAttribute('x-label')+"</b>");
									$(c.parentNode)
										.remove();
								});
						}			
					});
				}												
			}						
		});					
	};

	var IFRAME = 0;
	window.module_frame = function( url, id, a ){
		a.loading = true;
		if( !a.iframe ){
			a.iframe = document.createElement('iframe');
			a.iframe.name = 'ajaxiframe'+IFRAME++;
			a.iframe.style.display="none";

			$(a.iframe).appendTo('body');
		}
		
		$("<form>")
			.set('action',url)
			.set('method','post')	
			.set('target',a.iframe.name)
			.each(function(){
				var fo =this;
				
				$('#'+id)
					.find('input[type=checkbox]')
					.each(function(){
						$(fo).append(this.cloneNode(true))
					});				
				
				this.submit();
			});			
	}
	
	window.module_ajax = function( url, id, obj ){
		if( obj.xhr ){
			obj.xhr.stop();
		}
		var params = $('#'+id).query();
		params['_json'] = 'yes';
		
		obj.xhr = $.Ajax(url, {
			type:'POST',
			data:params,		
			create:	ajax_show,
			complete: ajax_hide,
			error: function(){
				$.Alert("Không thể tải dữ liệu.");
				ajax_hide();
			},			
			success: function(data){
				try{
					eval("var json_data = "+data+";");
				}catch(e){
					$.Alert( data );
					return false;
				}
				updateContent(null,json_data);
			}
		});
		return false;
	};	

	var obj_tool = null;
	$(document)
		.onMousemove(function( event ){
			if(!document.body)
				return;
			if( !obj_tool ){
				obj_tool = $("<div class='tooltip' style='position:absolute;top:10px;left:10px;z-index:10000'></div>")
					.appendTo("body")
					.k(0);
			}
			
			if( !obj_tool.activeElement || !$.Contains( obj_tool.activeElement, document.body) ){
				obj_tool.activeElement = null;
				$(obj_tool).css({display:'none'});
			}
			$(obj_tool)
				.each(function(){
					var t = event.pageY,
						l = event.pageX,
						h = $(window).height(), 
						w = $(window).width(),
						p = 15;
					
					var scrollTop = document.body.scrollTop;
					var scrollLeft = document.body.scrollLeft;
					
					var x = ( l < w/2 ) ? (l+p) : (l - $(this).width() - p );
					var y = ( t < h/2 ) ? (t+p) : (t - $(this).height()  - p);	

					$(this)
						.css({
							position:'absolute',
							top: y  +'px', 
							left: x + 'px'
						})
				});
		});
	
	APP_CALLBACK['tooltip'] = function(){
		$(this)
			.find(".x-tooltip")
			.onMouseover(function(){
				var elem = $(this).find(".x-toolbox").k(0);
				if( obj_tool.activeElement != elem ){
					obj_tool.activeElement = $(this).find(".x-toolbox").k(0);
					$( obj_tool )
						.htm( obj_tool.activeElement.innerHTML )
						.css({display:'inline-block'});
				}		
			})
			.onMouseout(function(){
				if( obj_tool.activeElement == $(this).find(".x-toolbox").k(0) ){
					obj_tool.activeElement = null;
					$(obj_tool).css({display:'none'});
				}
			});
	};
	
	var elem_pop = null;
	$(document)
		.onClick(function(){
			if( elem_pop ){
				if( elem_pop.clicked ){
					//$(elem_pop)
						//.find(".x-pop-content")
						//.show();				
				}else{
					$(elem_pop)
						.find(".x-pop-content")
						.hide();	
				}
				elem_pop.clicked = false;
			}	
		});
		
	APP_CALLBACK['popinline'] = function(){
		$(this)
			.find(".x-pop")
			.onClick(function(){
				elem_pop = this;
				elem_pop.clicked = true;
			})
			.each(function(){
				var elem = this;
				$(elem)
					.find(".x-pop-focus")
					.onFocus(function(){
						$(elem)
							.find(".x-pop-content")
							.show();			
					});
			});	
	};	

})(Owl);